package com.yphone.ui.login.model;

import android.content.Context;
import android.text.TextUtils;

import com.google.gson.reflect.TypeToken;
import com.iiordanov.bVNC.App;
import com.yphone.base.utils.SharepreferenceUtils;
import com.yphone.net.HttpConstants;
import com.yphone.net.IUIDataListener;
import com.yphone.net.bean.BaseResultWrapper;
import com.yphone.net.helper.ActivityHttpHelper;
import com.yphone.tools.MyLog;
import com.yphone.tools.SharepreferenConstants;
import com.yphone.ui.dialog.CommSmallLoadingDialog;
import com.yphone.ui.event.MyEvent;
import com.yphone.ui.login.bean.request.BaseHttpRequest;
import com.yphone.ui.login.bean.request.LoginRequestInfo;
import com.yphone.ui.login.contract.LoginActivityContract;
import com.yphone.ui.login.manager.LoginManager;
import com.yphone.ui.login.respone.UserInfo;
import com.yphone.ui.ysj.bean.RemoteConnectBean;
import com.yphone.utils.ToastUtils;

import org.greenrobot.eventbus.EventBus;

public class LoginActivityModel implements LoginActivityContract.Imodel {
    private ActivityHttpHelper<BaseResultWrapper<UserInfo>> httpHelper_Login;
    private IUIDataListener iuiDataListener_Login;
    public Context mContext;
    public int nextStep;
    private IUIDataListener wxDataListener;
    private ActivityHttpHelper<UserInfo> wxHttpHelper;
    private LoginRequestInfo loginRequestInfo;

    public LoginActivityModel(Context context) {
        this(context, 0);
    }

    public LoginActivityModel(Context context, int nextStep2) {
        this.wxDataListener = new IUIDataListener() {
            public void uiDataSuccess(Object object) {
//                if (object instanceof WXUserInfo) {
//                    LoginActivityModel.this.loginWXThirdStart((WXUserInfo) object);
//                }
            }

            public void uiDataError(Exception error) {
                MyLog.d(LoginActivityModel.class.getSimpleName(), "登陆异常！");
//                LoginManager.getInstance().clearUserInfo();
//                CommSmallLoadingDialog.dismissDialog();
            }
        };
        this.iuiDataListener_Login = new IUIDataListener() {
            public void uiDataSuccess(Object object) {
                CommSmallLoadingDialog.dismissDialog();
                BaseResultWrapper<UserInfo> userinfoWrapper = (BaseResultWrapper) object;
                if (userinfoWrapper == null || userinfoWrapper.code != 1) {
                    MyLog.d(LoginActivityModel.class.getSimpleName(), "登陆失败！");
                    if (!TextUtils.isEmpty(userinfoWrapper.msg)) {
                        ToastUtils.showLong((CharSequence) userinfoWrapper.msg);
                    }
                    LoginManager.getInstance().clearUserInfo();
                    CommSmallLoadingDialog.dismissDialog();
                    return;
                }
                if (!TextUtils.isEmpty(userinfoWrapper.msg)) {
                    ToastUtils.showLong((CharSequence) userinfoWrapper.msg);
                }
                MyLog.d(LoginActivityModel.class.getSimpleName(), "登陆成功！");
                LoginManager.getInstance().clearUserInfo();
                UserInfo userInfo = userinfoWrapper.data;
//                LoginActivityModel.this.setJpush(userInfo.UCID, userInfo.RealUCID);

                SharepreferenceUtils.saveClass(App.getContext(), SharepreferenConstants.SHARE_NAME_FILE, SharepreferenConstants.USER_INFO_NODE, userInfo);
//                if (userInfo.Bulletin != null) {
//                    SharepreferenceUtils.setSharePreferencesToInt(SharepreferenConstants.BULLETINID, userInfo.Bulletin.BulletinId);
//                }
//                if (LoginActivityModel.this.loginRequestInfo != null) {
//                    LoginActivityModel.this.saveSuccessAccount(LoginActivityModel.this.loginRequestInfo.PhoneNumber, LoginActivityModel.this.loginRequestInfo.PassWord);
//                }

                EventBus.getDefault().post(new MyEvent.LoginSuccessEvent(userInfo, LoginActivityModel.this.nextStep));
                EventBus.getDefault().post(new MyEvent.updateInit());
            }

            public void uiDataError(Exception error) {
                MyLog.d(LoginActivityModel.class.getSimpleName(), "登陆异常！");
                CommSmallLoadingDialog.dismissDialog();
            }
        };
        this.mContext = context;
        this.nextStep = nextStep2;
    }

    public void requestLoginToSer(LoginRequestInfo loginRequestInfo2) {
        try {
            if (this.httpHelper_Login == null) {
                this.httpHelper_Login = new ActivityHttpHelper<>(new TypeToken<BaseResultWrapper<UserInfo>>() {
                });
            }
            this.loginRequestInfo = loginRequestInfo2;
            BaseHttpRequest baseHttpRequest = new BaseHttpRequest();
            this.httpHelper_Login.UpdateUIDataListener(this.iuiDataListener_Login);
            this.httpHelper_Login.sendPostRequest(HttpConstants.user_name_login, baseHttpRequest.toMapPrames(loginRequestInfo2), HttpConstants.TIME_OUT3);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}